package com.sjzl.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.sjzl.entity.Board;
import com.sjzl.entity.Post;
import com.sjzl.entity.UploadFile;
import com.sjzl.entity.User;
import com.sjzl.service.BoardService;
import com.sjzl.service.PostService;
import com.sjzl.service.UploadFileService;
import com.sjzl.util.Pagination;

@Component("postAction")
@Scope("prototype")
public class PostAction extends BaseAction {
	
	@Resource(name="postServiceImpl")
	private PostService postService;
	
	@Resource(name="boardServiceImpl")
	private BoardService boardService;
	
	@Resource(name="uploadFileServiceImpl")
	private UploadFileService uploadFileService;
	
	private Pagination pagination;
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	private Post post;
	public Post getPost() {
		return post;
	}
	public void setPost(Post post) {
		this.post = post;
	}
	
	private Integer post_id;
	public Integer getPost_id() {
		return post_id;
	}
	public void setPost_id(Integer post_id) {
		this.post_id = post_id;
	}

	private List<Post> postList;
	public List<Post> getPostList() {
		return postList;
	}
	public void setPostList(List<Post> postList) {
		this.postList = postList;
	}
	
	private Integer board_id;
	public Integer getBoard_id() {
		return board_id;
	}
	public void setBoard_id(Integer board_id) {
		this.board_id = board_id;
	}
	
	private Board board;
	public Board getBoard() {
		return board;
	}
	public void setBoard(Board board) {
		this.board = board;
	}
	
	private String boardName;
	public void setBoardName(String boardName) {
		this.boardName = boardName;
	}

	private List<Board> boardNavList;
	public List<Board> getBoardNavList() {
		return boardNavList;
	}
	
	private List<Board> boardList;
	public List<Board> getBoardList() {
		return boardList;
	}
	
	private File[] files; //实际上传的文件
	public File[] getFiles() {
		return files;
	}
	public void setFiles(File[] files) {
		this.files = files;
	}
	
	private String query;
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	
	public String list() throws IOException{
		board = boardService.get(Board.class, board_id);
		totalCount = postService.getTotalCount("Post", "board = ", board_id);
		pagination = new Pagination(pageNo, 8, totalCount, "post_list?board_id=" + board_id);
		//此if语句段用于处理用户点击了最跟版块时使用
		if(board.getBoard() == null){
			response.sendRedirect("board_list");
			return null;
		}
		try {
			postList = postService.getPagePostListByBoardId(board_id, pageNo, 8);
		} catch (Exception e) {
			/*actionContext.getSession().put("errorMsg", "该版块下没有所属主题帖子！请与超级版主联系！");
			response.sendRedirect("error.jsp");
			return null;*/
			return "post_list";
		}
		this.setTitle(board.getName());
		boardNavList = boardService.getBoardNav(board_id);
		return "post_list";
	}
	
	public String searchPost(){
		totalCount = postService.getTotalCount("Post", "name like '%"+ query +"%'");
		pagination = new Pagination(pageNo, 10, totalCount, "post_searchPost?query=" + query);
		postList = postService.pageDate("from Post p where p.name like '%"+ query +"%'", pageNo, 10);
		if(postList.size() == 0){
			this.setMessage("对不起！没有您要搜索的结果");
		}
		return "search_success";
	}
	
	@Transactional
	@SuppressWarnings("unchecked")
	public String create() throws IOException {
		board = boardService.getBoardByName(boardName);
		if(board == null){
			board = boardService.get(Board.class, board_id);
		}
		User user = (User) httpSession.getAttribute("sessionUser");
		List<UploadFile> uploadFiles = (List<UploadFile>) ActionContext.getContext().getSession().get("sessionFileUploads");
		if(uploadFiles.isEmpty()||uploadFiles.size()==0){
			postService.create(post, board, user); 
		}else{
			postService.create(post, board, user);  
			Post p =postService.getLast(null);//获取最后一条帖子，即上面保存的帖子
			for(UploadFile uf : uploadFiles){
				uf.setPost(p);
				uploadFileService.update(uf);
			}
			ActionContext.getContext().getSession().remove("sessionFileUploads");
		}
		postList = postService.list("from Post where id = (select max(id) from Post)");
		post_id = postList.get(0).getId();
		response.sendRedirect("reply_list?post_id="+post_id);
		return null;

	}
	
	public String createUI() {
		List<UploadFile> sessionFileUploads =new ArrayList<UploadFile>();
		ActionContext.getContext().getSession().put("sessionFileUploads", sessionFileUploads);
		boardList = boardService.getSubBoardListByBoardId(board_id);
		board = boardService.get(Board.class, board_id);
		this.setTitle("创建--"+board.getName()+"--版块下的主题");
		if(board.getSubBoard().size() == 0){
			this.setMessage("----------注：您已经选择了最底层的版块，因此不用再选择子版块下拉表");
		}
		return "createUI";
	}
	
	public String delete() throws IOException{
		try {
			if(!postService.deletePost(post_id)){
				actionContext.getSession().put("errorMsg", "对不起，您不是该帖子所属版块的版主，不具有删除权限！请与超级版主联系！");
				response.sendRedirect("error.jsp");
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionContext.getSession().put("errorMsg", "此帖不可删除，具体原因请与超级版主联系！");
			response.sendRedirect("error.jsp");
			return null;
		}
		response.sendRedirect(historyURI);
		return null;
	}
	
}
